home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 10 - Networking / NovelNetwar / main.c < prev    next >
C/C++ Source or Header  |  1995-05-12  |  5KB  |  276 lines

  1. //    The main routine!
  2.  
  3. #include "NovelNetwar.h"
  4.  
  5.  
  6. static WindowPtr    lastFront = 0L;
  7.  
  8.  
  9.  
  10. static void InitToolbox(void);
  11. static void ChooseConnectionMethod(void);
  12.  
  13.  
  14.  
  15.  
  16. main()
  17. {
  18. EventRecord        theEvent;
  19. char            theChar;
  20. Point            thePt;
  21. OSErr            errCode;
  22. int                i;
  23.  
  24.  
  25.     InitToolbox();
  26.  
  27.     globalsInit();
  28.     appleEventsInit();
  29.     cursorsInit();
  30.     windowsInit();
  31.     helpInit();
  32.     menusInit();
  33.     gameplayInit();
  34.     appletalkingInit();
  35.     serialInit();
  36.     
  37.     ChooseConnectionMethod();
  38.     
  39.     globalsStartup();
  40.     appleEventsStartup();
  41.     cursorsStartup();
  42.     helpStartup();
  43.     windowsStartup();
  44.     menusStartup();
  45.     gameplayStartup();
  46.     appletalkingStartup();
  47.     serialStartup();
  48.     
  49.  
  50.     if (connectionMethod == SERIAL)
  51.     {
  52.         errCode = serialConnectToOpponents();
  53.         
  54.         if (errCode != noErr)
  55.         {
  56.             ErrorAlert(errorMessage);
  57.         }
  58.     }
  59.     
  60.     else if (connectionMethod == APPLETALK)
  61.     {
  62.         errCode = appleTalkingConnectToOpponents();
  63.         
  64.         if (errCode != noErr)
  65.         {
  66.             ErrorAlert(errorMessage);
  67.         }
  68.     }
  69.     
  70.     
  71.     
  72.     while (alive)
  73.     {
  74.         if (IAmFrontApp == true)
  75.         {
  76.             GetMouse(&thePt);
  77.             
  78.             cursorsMaintainCursor(thePt);
  79.             
  80.             gameplayIdle();
  81.         }
  82.         
  83.         
  84.         if (CapsLock() == false)
  85.         {
  86.             WaitNextEvent(everyEvent,&theEvent,0L,0L);
  87.             
  88.             switch (theEvent.what)
  89.             {
  90.                 case mouseDown:
  91.                     DoMouseDown(&theEvent);
  92.                     break;
  93.                 
  94.                 case keyDown:
  95.                 case autoKey:
  96.                     theChar = theEvent.message & charCodeMask;
  97.                     
  98.                     if (theEvent.modifiers & cmdKey)
  99.                     {
  100.                         DoCommand(MenuKey(theChar));
  101.                     }
  102.                     
  103.                     ObscureCursor();
  104.                     
  105.                     break;
  106.                     
  107.                 case activateEvt:
  108.                     if (IsOurWindow((WindowPtr) theEvent.message) == noErr)
  109.                     {
  110.                         if (!(theEvent.modifiers & activeFlag))
  111.                             lastFront = (WindowPtr) theEvent.message;
  112.                         
  113.                         DoActivateDeactivate((WindowPtr) theEvent.message,(char) (theEvent.modifiers & activeFlag));
  114.                     }
  115.                     
  116.                     break;
  117.                     
  118.                 case updateEvt: 
  119.                     if (IsOurWindow((WindowPtr) theEvent.message) == noErr) 
  120.                         UpdateWindow((WindowPtr) theEvent.message);
  121.                     
  122.                     break;
  123.                 
  124.                 case  osEvt:
  125.                     theChar = (theEvent.message & 0xFF000000) >> 24;
  126.                     
  127.                     if (theChar == suspendResumeMessage)
  128.                     {
  129.                         if (theEvent.message & 0x01)    /* resume */
  130.                         {
  131.                             if (IsOurWindow(lastFront) == noErr)
  132.                             {
  133.                                 IAmFrontApp = true;
  134.                                 
  135.                                 DoActivateDeactivate(lastFront,true);
  136.                             }
  137.                         }
  138.                         
  139.                         else    /* suspend */
  140.                         {
  141.                             if (IsOurWindow((WindowPtr) FrontWindow()) == noErr)
  142.                             {
  143.                                 lastFront = (WindowPtr) FrontWindow();
  144.                                 
  145.                                 IAmFrontApp = false;
  146.                                 
  147.                                 DoActivateDeactivate(lastFront,false);
  148.                                 
  149.                                 InitCursor();
  150.                             }
  151.                         }
  152.                     }
  153.                     
  154.                     break;
  155.             }
  156.         }
  157.     }
  158.     
  159.     DoShutDown();
  160.     
  161.     return(0);
  162. }
  163.  
  164.  
  165.  
  166.  
  167. static void InitToolbox(void)
  168. {
  169.     InitGraf((Ptr) &qd.thePort);
  170.     InitFonts();
  171.     InitWindows();
  172.     InitMenus();
  173.     FlushEvents(everyEvent,0);
  174.     TEInit();
  175.     InitDialogs(0L);
  176.     InitCursor();
  177. }
  178.  
  179.  
  180.  
  181.  
  182. static void ChooseConnectionMethod(void)
  183. {
  184. DialogPtr        theDPtr,tempDPtr;
  185. short            itemHit, type;
  186. Handle            theItem;
  187. Rect            tempRect;
  188. GrafPtr            oldPort;
  189. char            tempString[256];
  190. EventRecord        theEvent;
  191.  
  192.     GetPort(&oldPort);
  193.     
  194.     InitCursor();
  195.     
  196.     if (!(theDPtr = GetNewDialog(CHOOSEMETHODDLOG, nil,(WindowPtr) -1L)))
  197.     {
  198.         FatalError("Can't allocate memory for Choose Connection Method dialog....");
  199.     }
  200.     
  201.     SetPort(theDPtr);
  202.     
  203.     CenterWindow(theDPtr);
  204.     ShowWindow(theDPtr);
  205.     
  206.     ((DialogPeek) theDPtr)->aDefItem = 4;
  207.     
  208.     GetDItem(theDPtr, 5, &type, &theItem, &tempRect);
  209.     SetDItem(theDPtr, 5, type,(Handle) HiliteDefaultButton, &tempRect);
  210.     
  211.     GetDItem(theDPtr, 2, &type, &theItem, &tempRect);
  212.     SetControlValue((ControlHandle) theItem,0);
  213.     
  214.     GetDItem(theDPtr, 3, &type, &theItem, &tempRect);
  215.     SetControlValue((ControlHandle) theItem,0);
  216.  
  217.     do 
  218.     {
  219.         ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
  220.         
  221.         if (itemHit == 3)
  222.         {
  223.             if (connectionMethod != SERIAL)
  224.             {
  225.                 connectionMethod = SERIAL;
  226.                 
  227.                 GetDItem(theDPtr, 3, &type, &theItem, &tempRect);
  228.                 SetControlValue((ControlHandle) theItem,1);
  229.                 
  230.                 GetDItem(theDPtr, 2, &type, &theItem, &tempRect);
  231.                 SetControlValue((ControlHandle) theItem,0);
  232.             }
  233.         }
  234.         
  235.         else if (itemHit == 2)
  236.         {
  237.             if (connectionMethod != APPLETALK)
  238.             {
  239.                 connectionMethod = APPLETALK;
  240.                 
  241.                 GetDItem(theDPtr, 3, &type, &theItem, &tempRect);
  242.                 SetControlValue((ControlHandle) theItem,0);
  243.                 
  244.                 GetDItem(theDPtr, 2, &type, &theItem, &tempRect);
  245.                 SetControlValue((ControlHandle) theItem,1);
  246.             }
  247.         }
  248.         
  249.     } while (itemHit != 4);
  250.     
  251.     DisposDialog(theDPtr);
  252.     SetPort(oldPort);
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. void DoShutDown(void)
  261. {
  262.     serialShutdown();
  263.     appletalkingShutdown();
  264.     gameplayShutdown();
  265.     windowsShutDown();
  266.     menusShutDown();
  267.     cursorsShutDown();
  268.     helpShutDown();
  269.     appleEventsShutDown();
  270.     globalsShutDown();
  271. }
  272.  
  273.  
  274.  
  275.  
  276.